<?php
/**
 * Created by PhpStorm.
 * User: dy
 * Date: 2019/12/19
 * Time: 16:51
 */
namespace app\admin\validate;
use app\admin\enum\AdminEnum;
use app\admin\model\Admin;
use app\common\validate\BaseValidate;

class AdminValidate extends BaseValidate
{
    public function __construct()
    {
        parent::__construct();
        $this->rule['status|是否禁用'] .=  AdminEnum::getEnumIdsToStr('status');
    }
    protected $rule = [
        'id'=>'require|number',
        'username|用户名'=>'require|min:5|unique:admin',
        'nickname|昵称'=>'max:32',
        'phone|手机号码'=>'mobile',
        'email'=>'email',
        'old_password|旧密码'=>'min:6|check_password:',
        'password|密码'=>'min:6|requireWithout:id',
        'confirm_password|确认密码'=>'confirm:password|requireWith:password',
        'parent_id|上级管理员'=>'number',
        'status|是否禁用'=>'number|in:',
    ];
    protected $scene = [
        'edit'=>['id','username','nickname','phone','email','parent_id','status','password','confirm_password'],
        'add'=>['username','nickname','phone','email','parent_id','status','password','confirm_password'],
        'edit_password'=>['id','password','old_password','confirm_password']
    ];

    protected function check_password($value,$rule,$data){
        if(!isset($data['id']) || empty($data['id'])){
            return '参数异常';
        }
        $admin_info = Admin::find($data['id']);
        if(empty($admin_info)){
            return '用户不存在';
        }
        if(!password_verify($value,$admin_info['password'])){
            return '旧密码错误';
        }
        return true;
    }
}